package gowcage.slidemenu.custom;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import gowcage.utils.ScreenUtils;

/**
 * Created by Administrator on 2017/4/13.
 * 自定义侧滑菜单控件
 */

public class SlidingMenu extends HorizontalScrollView {
    /**
     * 屏幕宽度
     */
    private int mScreenWidth;
    /**
     * dp
     */
    private int mMenuRightPadding = 100;
    /**
     * 菜单的宽度
     */
    private int mMenuWidth;
    private int mHalfMenuWidth;
    //菜单是否显示
    private boolean isShow = false;
    //有效切换的距离
    private final int distance = 60;

    private boolean once;

    public SlidingMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        mScreenWidth = ScreenUtils.getScreenW(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //显示的设置一个宽度
        if (!once) {
            LinearLayout wrapper = (LinearLayout) getChildAt(0);
            ViewGroup menu = (ViewGroup) wrapper.getChildAt(0);
            ViewGroup content = (ViewGroup) wrapper.getChildAt(1);
            // dp to px
            mMenuRightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mMenuRightPadding,
                    content.getResources().getDisplayMetrics());

            mMenuWidth = mScreenWidth / 3;
            mHalfMenuWidth = mMenuWidth / 2;
            menu.getLayoutParams().width = mMenuWidth;
            content.getLayoutParams().width = mScreenWidth;
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
//        this.scrollTo(mMenuWidth, 0);
        if (changed) {
            // 将菜单隐藏
            this.scrollTo(mMenuWidth, 0);
            once = true;
        }
    }

    int startX = 0, startY;
    int endX, endY;

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int action = ev.getAction();

        switch (action) {
            case MotionEvent.ACTION_UP:
                int scrollX = getScrollX();
                if (isShow) {
                    if (scrollX > distance) {
                        Log.i("SlidingMenu-->", "hide");
                        this.smoothScrollTo(mMenuWidth, 0);
                        isShow = false;
                    } else {
                        this.smoothScrollTo(0, 0);
                    }
                } else {
                    if (mMenuWidth - scrollX > distance) {
                        isShow = true;
                        Log.i("SlidingMenu-->", "show");
                        this.smoothScrollTo(0, 0);
                    } else {
                        this.smoothScrollTo(mMenuWidth, 0);
                    }
                }
                Log.i("SlidingMenu-->", "------------------------------------------------------");
                return true;
        }
        return super.onTouchEvent(ev);
    }

    /**
     * 切换菜单状态
     */
    public void toggle()
    {
        if (isShow)
        {
            this.smoothScrollTo(mMenuWidth, 0);
            isShow = false;
        } else
        {
            this.smoothScrollTo(0, 0);
            isShow = true;
        }
    }

}
